package net.zisen.cloud.base.msg;


/**
 * 业务层返回结果
 *
 * @author tim
 * @date 2019-03-27
 */

public class ResultMsg<T> implements BaseMsg {

    private boolean success;
    private int resultCode;
    private String resultMsg;
    private T data;

    /**
     * @param success    是否成功
     * @param resultCode 结果编码:0-成功；>0-失败
     * @param resultMsg  结果描述
     * @param data       业务数据
     */
    public ResultMsg(boolean success, int resultCode, String resultMsg, T data) {
        this.success = success;
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.data = data;
    }


    /**
     * @param success    是否成功
     * @param resultCode 结果编码:0-成功；>0-失败
     * @param resultMsg  结果描述
     */
    public ResultMsg(boolean success, int resultCode, String resultMsg) {
        this.success = success;
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }


    /**
     * @param success    是否成功
     * @param resultCode 结果编码:0-成功；>0-失败
     */
    public ResultMsg(boolean success, int resultCode) {
        this.success = success;
        this.resultCode = resultCode;
    }


    /**
     * @param success 是否成功
     */
    public ResultMsg(boolean success) {
        this.success = success;
    }

    public ResultMsg() {
    }

    /**
     * 是否成功
     *
     * @return true:成功;false:失败
     */
    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * 返回结果编码
     *
     * @return 0:成功;>0:失败
     */
    public int getResultCode() {
        return resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public static ResultMsg ok() {
        return new ResultMsg(true, 0);
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> ResultMsg<T> ok(String resultMsg, T data) {
        return new ResultMsg<T>(true, 0, resultMsg, data);
    }

    public static <T> ResultMsg<T> ok(String resultMsg, int resultCode, T data) {
        return new ResultMsg<T>(true, resultCode, resultMsg, data);
    }

    public static <T> ResultMsg<T> ok(T data) {
        return new ResultMsg<T>(true, 0, null, data);
    }

    public static <T> ResultMsg<T> ok(String resultMsg) {
        return new ResultMsg(true, 0, resultMsg, null);
    }

    public static ResultMsg fail() {
        return new ResultMsg(false, 1);
    }

    public static <T> ResultMsg<T> fail(String resultMsg, T data) {
        return new ResultMsg<T>(false, 0, resultMsg, data);
    }

    public static <T> ResultMsg<T> fail(String resultMsg, int resultCode, T data) {
        return new ResultMsg<T>(false, resultCode, resultMsg, data);
    }

    public static <T> ResultMsg<T> fail(T data) {
        return new ResultMsg<T>(false, 0, null, data);
    }

    public static <T> ResultMsg<T> fail(String resultMsg) {
        return new ResultMsg(false, 0, resultMsg, null);
    }

    /**
     * 结果描述
     *
     * @return
     */
    @Override
    public String getResultMsg() {
        return resultMsg;
    }

    public ResultMsg resultMsg(String resultMsg) {
        this.setResultMsg(resultMsg);
        return this;
    }

    public ResultMsg resultCode(int resultCode) {
        this.setResultCode(resultCode);
        return this;
    }

    public ResultMsg<T> data(T data) {
        this.setData(data);
        return this;
    }


}
